package com.szy.web.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szy.web.dao.CommentDAO;
import com.szy.web.model.Page;
import com.szy.web.util.TextUtility;

public class showCommentsServlet extends HttpServlet implements Servlet {
	int pageSize = 10;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		 if ( request.getSession().getAttribute("Login") != null) 
		   {
			  
				String message = (String) request.getSession().getAttribute("Login");
				if (!message.equals("true"))
				{
					 //如果登录标志不为true则跳转回登录页面
					 response.sendRedirect("index.jsp");
					 return;
				}
				else
				{
					request.getSession().setAttribute("Login","true");
				}   
		   }
		   else
		   {
		    	response.sendRedirect("index.jsp");
		    	return;
		   }
		String method = request.getParameter("method");
		if (method.equalsIgnoreCase("comment")) 
		{
			String page = request.getParameter("currentPage");
			String nidtmp = request.getParameter("nid");
			System.out.println("page  = "+ page);
			int nid =  TextUtility.String2Int(nidtmp);
			int currentPage = 1;
			int pageSize = 10;
			if ( page != null )  
			{
				currentPage = Integer.parseInt(page);
				System.out.println("currentPage  = "+ currentPage);
			}
			try {
				CommentDAO  commentDao = new CommentDAO(); 
				Page pageModel = commentDao.showComment(currentPage, pageSize,nid );// 查询新闻从currPage到currPage+pageSize
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				System.out.println("查看评论OK");
				request.getRequestDispatcher("showComment.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
			}
			
		}
		else if (method.equalsIgnoreCase("commentdelete")) 
		{
			int cid=Integer.parseInt(request.getParameter("cid"));
			int currentPage = Integer.parseInt(request.getParameter("currentPage"));
			int nid = Integer.parseInt(request.getParameter("nid"));
			try {
				CommentDAO  commentDao = new CommentDAO(); 
				commentDao.deleteComment( cid );
				Page pageModel = commentDao.showComment( currentPage, pageSize, nid);
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getRequestDispatcher("showComment.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}
		else if (method.equalsIgnoreCase("choise")) {
			String page = request.getParameter("currentPage");
			String nidtmp =  request.getParameter("nidtmp");
			System.out.println("page  = "+ page);
			System.out.println("nidtmp = "+ nidtmp);
			int nid =  TextUtility.String2Int(nidtmp);
			int currentPage = 1;
			int pageSize = 10;
			if ( page != null ) {
				currentPage = Integer.parseInt(page);
				System.out.println("currentPage  = "+ currentPage);
			}
			try {
				CommentDAO  commentDao = new CommentDAO(); 
				Page pageModel = commentDao.showComment(currentPage, pageSize,nid );// 查询新闻从currPage到currPage+pageSize
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getRequestDispatcher("showComment.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
			}
			
		}
		
	}

}
